<template>
  <div>
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard" body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容" v-model="queryInfo.srm" clearable></el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" type="primary" @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" @click="addDialogVisible = true" type="primary">新增给药途径</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="TableCard" :body-style="{ padding: '0px' }">
      <div>
        <el-table
          :data="TableData"
          border
          :header-cell-style="{ background: '#3F9DFD', color: '#FFF' }"
          size="medium"
          stripe
          style="width: 100%;font-size:12px"
        >
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="name" label="操作" width="60">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="showSysDictRouteOne(scope.row.id)">编辑</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="60">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="open(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="showAddDialogVisible(scope.row.id)"
              >新增已有给药途径</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="routeId" label="给药途径id"></el-table-column>
          <el-table-column prop="routeName" label="给药途径名称"></el-table-column>
          <el-table-column prop="searchCode" label="搜索码"></el-table-column>
          <el-table-column prop="dupcRouteType" label="重复用药"></el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          background
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog title="编辑给药途径信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="RouteInfo">
        <el-form-item label="给药途径编码">
          <el-input v-model="RouteInfo.routeId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="给药途径名称">
          <el-input v-model="RouteInfo.routeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复用药(仅填写1-12数字)">
          <el-input v-model="RouteInfo.dupcRouteType" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="UpdateRouteOne">保 存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增给药途径信息" :visible.sync="addDialogVisible" width="30%">
      <el-form :model="addRouteInfo">
        <el-form-item label="给药途径名称">
          <el-input v-model="addRouteInfo.routeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复用药(仅填写1-12数字)">
          <el-input v-model="addRouteInfo.dupcRouteType" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addNewRouteOne">新 增</el-button>
        <el-button @click="addDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增已有给药途径信息" :visible.sync="addOldDialogVisible" width="30%">
      <el-form :model="addOldRouteDate">
        <el-form-item label="给药途径名称">
          <el-input v-model="addOldRouteDate.routeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="重复用药(仅填写1-12数字)">
          <el-input v-model="addOldRouteDate.dupcRouteType" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addOldRouteOne">新 增</el-button>
        <el-button @click="addOldDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  showDictRouteAll,
  showSysDictRouteOne,
  updateSysDictRouteOne,
  addNewRouteOne,
  deleteRouteOne,
  addOldRouteOne
} from "@/api/rad";
export default {
  data() {
    return {
      queryInfo: {
        srm: "",
        pageList: 1,
        pageSize: 20
      },
      total: 0,
      TableData: [],
      dialogVisible: false,
      RouteInfo: {
        id: "",
        routeId: "",
        routeName: "",
        dupcRouteType: 0
      },
      addDialogVisible: false,
      addRouteInfo: {
        routeName: "",
        dupcRouteType: ""
      },
      addOldRouteDate: {
        routeId: "",
        routeName: "",
        dupcRouteType: ""
      },
      addOldDialogVisible: false
    };
  },
  created: function() {
    this.getTableList();
  },
  methods: {
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
    },

    handleCurrentChange(newPage) {
      this.queryInfo.pageList = newPage;
    },
    async getSearchList() {
      this.getTableList();
    },
    async getTableList() {
      let res = await this.$rad.showDictRouteAll({
        srm: this.queryInfo.srm,
        pageList: this.queryInfo.pageList,
        pageSize: this.queryInfo.pageSize
      });
      this.TableData = res.rows;
      this.total = res.total;
      console.log(res);
    },
    async showSysDictRouteOne(id) {
      this.RouteInfo.id = id;
      let res = await this.$rad.showSysDictRouteOne({
        id: id
      });
      this.RouteInfo.routeId = res.data.routeId;
      this.RouteInfo.routeName = res.data.routeName;
      this.RouteInfo.dupcRouteType = res.data.dupcRouteType;
      console.log(this.RouteInfo);
      this.dialogVisible = true;
    },
    async UpdateRouteOne() {
      let res = await this.$rad.updateSysDictRouteOne({
        id: this.RouteInfo.id,
        routeId: this.RouteInfo.routeId,
        routeName: this.RouteInfo.routeName,
        dupcRouteType: this.RouteInfo.dupcRouteType
      });
      if (res.code == 1) {
        this.$message.success(res.msg);
        this.dialogVisible = false;
        this.getTableList();
      } else {
        this.$message.error(res.msg);
      }
    },
    async addNewRouteOne() {
      let res = await this.$rad.addNewRouteOne({
        routeName: this.addRouteInfo.routeName,
        dupcRouteType: this.addRouteInfo.dupcRouteType
      });
      // console.log(res)
      if (res.code == 1) {
        this.$message.success(res.msg);
        this.addDialogVisible = false;
        this.getTableList();
      } else {
        this.$message.error(res.msg);
      }
    },
    open(id) {
      this.$confirm("此操作将永久删除该给药途径记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteRouteOne(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async deleteRouteOne(id) {
      let res = await this.$rad.deleteRouteOne({
        id: id
      });
      if (res.code == 1) {
        this.$message.success(res.msg);
        this.getTableList();
      } else {
        this.$message.error(res.msg);
      }
    },
    showAddDialogVisible(id) {
      this.addOldRouteDate.routeId = id;
      this.addOldDialogVisible = true;
    },
    // 新增已有给药途径
    async addOldRouteOne() {
      let res = await this.$rad.addOldRouteOne({
        routeId: this.addOldRouteDate.routeId,
        routeName: this.addOldRouteDate.routeName,
        dupcRouteType: this.addOldRouteDate.dupcRouteType
      });
      if (res.code == 1) {
        this.$message.success(res.msg);
        this.addOldDialogVisible = false;
        this.getTableList();
      } else {
        this.$message.error(res.msg);
      }
    }
  }
};
</script>
<style>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
</style>